/*
 * @Descripttion: 基础功能的抽象模板方法
 * @version: 1.0
 * @LastEditors: lzt
 * @LastEditTime: 2024-06-12
 */

import {ElNotification} from "element-plus";
import i18n from "@/locales";

const BaseTemplate = {
    /**
     * 统一导出处理
     * @param _this
     * @param exportApi
     * @param attachParams
     * @param exportBtnRef
     * @param exportBtnIndex
     * @param exTableRef
     * @returns {Promise<void>}
     */
    export: async function (_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef) {
        if (!_this || !exportApi) {
            await _this.$alert(_this.$t('common.params_error'), _this.$t('common.params_error'), {type: 'warning'}).catch(() => {
            });
            return;
        }
        let queryPrams = {};
        if (exTableRef) {
            Object.assign(queryPrams, exTableRef.queryParams());
            let selections = exTableRef.selections();
            if (selections.length === 0) {
                let confirm = await _this.$confirm(_this.$t('common.export_confirm_export_all_data'), _this.$t('common.tip'), {type: 'warning',confirmButtonText: _this.$t('common.yes'), cancelButtonText: _this.$t('common.no')}).then(() => {
                    return true;
                }).catch(() => {
                    return false;
                });
                if (!confirm) return;
            } else {
                let confirm = await _this.$confirm(_this.$t('common.export_confirm_export_checked_data', {count: selections.length}), _this.$t('common.tip'), {type: 'warning',confirmButtonText: _this.$t('common.yes'), cancelButtonText: _this.$t('common.no')}).then(() => {
                    return true;
                }).catch(() => {
                    return false;
                });
                if (!confirm) return;
            }
            queryPrams.ids = selections.map(item => {
                return item.id;
            });

			//根据表格的列配置，生成导出列
			let columns = [];
			let widgetColumns = exTableRef.columns;
			if(widgetColumns){
				widgetColumns.forEach(item=>{
					if(!item.fixedValue && item.name !== '操作'){
						let column = {
							property:item.property,
							label:item.label||item.name,
							formatter:item.formatter,
							key:item.key
						}
						columns.push(column);
					}
				});
			}

			queryPrams.exportColumns = columns;
        }
        if (attachParams) {
            Object.assign(queryPrams, attachParams);
        }
        if (exportBtnRef&&(exportBtnIndex !== undefined || true)) {
            exportBtnRef.loading(exportBtnIndex, true);
        }

        if(!queryPrams.exportName){
            //设置当前路由的名称为导出的名称，设置当前表格的列为导出的列
            queryPrams.exportName=_this.$route.meta.title||_this.$route.name;
        }

        try {
            let res = await exportApi.post(queryPrams);
            if (res.code === 2000) {
                await _this.$alert(_this.$t('common.export_request_success'), _this.$t('common.tip'), {type: 'success'}).catch(() => {
                });
            }
        } finally {
            if (exportBtnRef&&(exportBtnIndex !== undefined || true)) {
                exportBtnRef.loading(exportBtnIndex, false);
            }
        }
    },

    async validate(_this,dataForm) {
        if(dataForm){
            return dataForm.validate((valid, invalidFields) => {
                if (!valid) {
                    for(let prop in invalidFields){
                        _this.$message.error(invalidFields[prop][0].message);
                        break;
                    }
                }
                return valid;
            });
        }
    }
}


export default BaseTemplate
